//
//  LoadingView.m
//  FreeMessage
//
//  Created by Fox Guo on 12-8-28.
//  Copyright (c) 2012年 Fox. All rights reserved.
//

#import "HOLoadingView.h"
#import "HOUtils.h"
#import <QuartzCore/QuartzCore.h>
static int kMaxWidth = 200;
@implementation HOLoadingView
@synthesize hintPopupDelegate;
@synthesize isHintPopup;
- (id)init
{
    self = [super init];
    if(self)
    {
        CGRect screenBound = [[UIScreen mainScreen] bounds];
        CGSize screenSize = screenBound.size;
        CGFloat screenWidth = screenSize.width;
        int w = 150;
        self.frame = CGRectMake((screenWidth - w)/2, 155, w, 100);
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 10.0;
        activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityView.frame = CGRectMake(60,
                                        20,
                                        activityView.bounds.size.width,
                                        activityView.bounds.size.height);
        [self addSubview:activityView];
        loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(-10, 60, 170, 22)];
        loadingLabel.backgroundColor = [UIColor clearColor];
        loadingLabel.textColor = [UIColor whiteColor];
		loadingLabel.font = [UIFont systemFontOfSize:15];
        loadingLabel.adjustsFontSizeToFitWidth = YES;
        loadingLabel.textAlignment = NSTextAlignmentCenter;
        loadingLabel.text = NSLocalizedString(@"正在加载数据!", @"正在加载数据!");
        [self addSubview:loadingLabel];
        
    }
    return self;
}
- (void)setText:(NSString *)text
{
	loadingLabel.text = text;
}

- (id)initWithSizeForHint:(int)size
{
    self = [super init];
    if(self)
    {
        timeoutTimer = nil;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        CGRect screenBound = [[UIScreen mainScreen] bounds];
        CGSize screenSize = screenBound.size;
        CGFloat screenWidth = screenSize.width;
        self.frame = CGRectMake((screenWidth - kMaxWidth)/2, 155, kMaxWidth, 22);
        hintLabel = [[UITextView alloc] initWithFrame:CGRectMake(0, 4, kMaxWidth, 22)];
        hintLabel.backgroundColor = [UIColor clearColor];
        hintLabel.textColor = [UIColor whiteColor];
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 5.0;
        //        hintLabel.adjustsFontSizeToFitWidth = YES;
        hintLabel.font = [UIFont systemFontOfSize:14];
        hintLabel.textAlignment = NSTextAlignmentCenter;
        //        hintLabel.layer.borderWidth = 1.0f;
        //        hintLabel.layer.borderColor = [UIColor redColor].CGColor;
        [hintLabel setFont:[UIFont systemFontOfSize:size]];
        hintLabel.text = NSLocalizedString(@"提示", @"");
        hintLabel.frame = CGRectMake(0, 0, 10, 22);
        [self addSubview:hintLabel];
        hintLabel.userInteractionEnabled = NO;
        
    }
    return self;
}
- (id)initForHint
{
    return [self initWithSizeForHint:15];
}


- (void)show:(UIView *)atView
{
    if([self superview] == nil)
    {
        [atView addSubview:self];
    }
    if(activityView.isAnimating == NO)
    {
        [activityView startAnimating];
    }
    
}
- (void)startTimer:(NSTimeInterval)time
{
    if(timeoutTimer != nil)
    {
        [timeoutTimer invalidate];
        timeoutTimer = nil;
    }
    if(timeoutTimer == nil)
    {
        timeoutTimer = [NSTimer scheduledTimerWithTimeInterval: time
                                                        target: self
                                                      selector: @selector(stopTimer:)
                                                      userInfo: nil
                                                       repeats: NO];
    }
}

- (void)stopTimer:(NSTimer *)timer
{
    if(isHintPopup)
    {
        isHintPopup = NO;
        if([self.hintPopupDelegate respondsToSelector:@selector(hintDidHidden)])
        {
            [self.hintPopupDelegate hintDidHidden];
        }
    }
    [self hide];
    if(timeoutTimer != nil)
    {
        [timeoutTimer invalidate];
        timeoutTimer = nil;
    }
}
-(CGSize) getContentSize:(UITextView*) myTextView{
    return [myTextView sizeThatFits:CGSizeMake(myTextView.frame.size.width, FLT_MAX)];
}
- (void)show:(UIView *)atView content:(NSString *)content displayTime:(NSTimeInterval)time withFrame:(CGRect)frame
{
    hintLabel.frame = frame;
    if([self superview] == nil)
    {
        [atView addSubview:self];
    }
    hintLabel.text = content;
    
    
    CGSize newSize = [hintLabel sizeThatFits:CGSizeMake(kMaxWidth, MAXFLOAT)];
    CGRect newFrame = hintLabel.frame;
    newFrame.size = CGSizeMake(kMaxWidth, newSize.height);
    hintLabel.frame = newFrame;
    self.frame = CGRectMake(([HOUtils getScreenWidth] - kMaxWidth) / 2,
                            self.frame.origin.y,
                            kMaxWidth,
                            hintLabel.frame.size.height);
    
    [self startTimer:time];
}

- (CGFloat)getHintHeight
{
    return hintLabel.frame.size.height;
}
- (void)show:(UIView *)atView content:(NSString *)content displayTime:(NSTimeInterval)time
{
    [self show:atView content:content displayTime:time withFrame:CGRectMake(0, 0, kMaxWidth, self.frame.size.height)];
    
}

- (void)hide
{
    //    loadingLabel.text = @"test";
    if([activityView isAnimating])
        [activityView stopAnimating];
    if([self superview] != nil)
    {
        [self removeFromSuperview];
        
    }
    
}




@end
